package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Category;
import com.changgou.service.CategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品控制层
 *
 * @author gaofeng
 * @date 2025/4/20 10:51
 */
@Api(value = "CategoryController")
@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * Category分页条件搜索实现
     *
     * @param category 分页查询条件
     * @param page     当前页
     * @param size     每页显示条数
     * @return Result
     */
    @ApiOperation(value = "Category条件分页查询", notes = "分页条件查询Category方法详情", tags = {"CategoryController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Category对象", value = "传入JSON数据") Category category, @PathVariable int page, @PathVariable int size) {
        PageInfo<Category> pageInfo = categoryService.findPage(category, page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * Category分页搜索实现
     *
     * @param page 当前页
     * @param size 每页显示多少条
     * @return Result
     */
    @ApiOperation(value = "Category分页查询", notes = "分页查询Category方法详情", tags = {"CategoryController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        PageInfo<Category> pageInfo = categoryService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param category 多条件搜索条件
     * @return Result
     */
    @ApiOperation(value = "Category条件查询", notes = "条件查询Category方法详情", tags = {"CategoryController"})
    @PostMapping(value = "/search")
    public Result<List<Category>> findList(@RequestBody(required = false) @ApiParam(name = "Category对象", value = "传入JSON数据") Category category) {
        List<Category> list = categoryService.findList(category);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id 品牌id
     * @return Result
     */
    @ApiOperation(value = "Category根据ID删除", notes = "根据ID删除Category方法详情", tags = {"CategoryController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        //调用CategoryService实现根据主键删除
        categoryService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    /**
     * 修改Category数据
     *
     * @param category Category信息
     * @param id       主键id
     * @return Result
     */
    @ApiOperation(value = "Category根据ID修改", notes = "根据ID修改Category方法详情", tags = {"CategoryController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value = "/update/{id}")
    public Result update(@RequestBody @ApiParam(name = "Category对象", value = "传入JSON数据") Category category, @PathVariable Integer id) {
        category.setId(id);
        categoryService.update(category);
        return new Result<>(true, StatusCode.OK, "修改成功");
    }

    /**
     * 新增Category数据
     *
     * @param category 新增的category信息
     * @return Result
     */
    @ApiOperation(value = "Category添加", notes = "添加Category方法详情", tags = {"CategoryController"})
    @PostMapping(value = "/add")
    public Result add(@RequestBody @ApiParam(name = "Category对象", value = "传入JSON数据", required = true) Category category) {
        categoryService.add(category);
        return new Result<>(true, StatusCode.OK, "添加成功");
    }

    /***
     * 根据ID查询Category数据
     * @param id 主键id
     * @return Category信息
     */
    @ApiOperation(value = "Category根据ID查询", notes = "根据ID查询Category方法详情", tags = {"CategoryController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/findById/{id}")
    public Result<Category> findById(@PathVariable Integer id) {
        Category category = categoryService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", category);
    }

    /**
     * 查询Category全部数据
     *
     * @return Category集合
     */
    @ApiOperation(value = "查询所有Category", notes = "查询所Category有方法详情", tags = {"CategoryController"})
    @GetMapping(value = "/findAll")
    public Result<List<Category>> findAll() {
        List<Category> list = categoryService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 根据父节点id查询所有子节点
     *
     * @param pid 父节点id
     * @return com.changgou.entity.Result<java.util.List < com.changgou.goods.pojo.Category>>
     */
    @ApiOperation(value = "根据父节点id查询所有子节点", notes = "根据父节点id查询所有子节点", tags = {"CategoryController"})
    @GetMapping("/findByParentId/{pid}")
    public Result<List<Category>> findByParentId(@PathVariable(value = "pid") Integer pid) {
        return new Result<>(true, StatusCode.OK, "查询成功", categoryService.findByParentId(pid));
    }
}
